package com.net.xpay.partner.controller.partner;

import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.LocalDateUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.stat.PartnerUserDayStatManager;
import com.net.xpay.common.model.PartnerUserDayStatModel;
import com.net.xpay.common.model.PartnerUserMonthStatModel;
import com.net.xpay.common.model.PartnerUserSumModel;
import com.net.xpay.common.model.search.PartnerUserDayStatSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.partner.secutiry.RequestUser;
import com.net.xpay.partner.vo.PartnerUserDayStatVo;
import com.net.xpay.partner.vo.PartnerUserMonthStatVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author  on 20/03/2018.
 * 查询合伙人统计数据
 */
@RestController
@RequestMapping("/partner/partner-user-day-stat")
@Api(tags = "合伙人/合伙人的商户统计数据", description = "合伙人 统计数据 zc")
public class PartnerUserDayStatController {
    @Autowired
    private PartnerUserDayStatManager partnerUserDayStatManager;

    @Autowired
    private UserManager userManager;


    @ApiOperation("聚合搜索合伙人的商户数据")
    @PostMapping("/agg-search")
    @ResponseBody
    public ResultPagedList<PartnerUserDayStatVo> aggSearch(@ApiIgnore @RequestUser Partner partner, @RequestBody PartnerUserDayStatSearchModel searchModel) {
        searchModel.setPartnerId(partner.getId());
        List<PartnerUserDayStatModel> records = partnerUserDayStatManager.aggSearch(searchModel);
        long count = partnerUserDayStatManager.aggSearchCount(searchModel);
        List<PartnerUserDayStatVo> vos = BeanCopierUtil.batchCopy(PartnerUserDayStatModel.class, PartnerUserDayStatVo.class, records);
        for (PartnerUserDayStatVo partnerUserDayStatVo : vos) {
            User user = userManager.getById(partnerUserDayStatVo.getUserId());
            partnerUserDayStatVo.setUserName(user.getName());
        }

        PartnerUserSumModel partnerUserSumModel = partnerUserDayStatManager.aggSum(searchModel);

        ResultPagedList<PartnerUserDayStatVo> partnerUserDayStatVoResultPagedList = new ResultPagedList<>(vos, count, searchModel);
        partnerUserDayStatVoResultPagedList.setExtra(partnerUserSumModel);
        return partnerUserDayStatVoResultPagedList;
    }

    @ApiOperation("聚合搜索合伙人的商户月数据")
    @PostMapping("/agg-get-sum-data")
    @ResponseBody
    public ResultData<PartnerUserMonthStatVo> aggGetPartnerUserInfo(@ApiIgnore @RequestUser Partner partner) {

        LocalDateTime minEndTime = LocalDateUtil.getMonthStartDay(LocalDateTime.now());
        LocalDateTime maxEndTime = LocalDateUtil.getDayStart(LocalDateTime.now());
        Long registerCount = userManager.countRegister(partner.getId(), minEndTime,maxEndTime);


        PartnerUserMonthStatModel monthStatModel = partnerUserDayStatManager.sumPartnerUserDayStatInfo(partner.getId(), minEndTime.toLocalDate(),maxEndTime.toLocalDate());

        PartnerUserMonthStatVo vo = new PartnerUserMonthStatVo();

        vo.setServiceChargeFee(monthStatModel.getServiceChargeFee());
        vo.setTotalSuccessCount(monthStatModel.getTotalSuccessCount());
        vo.setTotalServicePrice(monthStatModel.getTotalServicePrice());
        vo.setRegisterCount(registerCount);

        vo.setUpdateTime(minEndTime.toLocalDate().toString().substring(0, 7) + "月");
        return new ResultData<>(vo);
    }

}
